﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using System.Xml.Serialization;

namespace MedReg.Classes
{
    [Serializable]
    [Table (Name = "Level")]
    public class Level: ISerializable
    {
        private long idField;

        private long parentField;

        private string nameField;

//        private EntitySet<LPU> _lpu = new EntitySet<LPU>();

        private static readonly Level theOneObject = new Level();

        public static Level GetLevel()
        {
            return theOneObject;
        }

/*        
        [Association(Name = "Level_LPU_FK1", Storage = "_lpu", ThisKey = "ID", OtherKey = "LPULevel")]
        public EntitySet<LPU> LPU
        {
            get 
            {
                return _lpu;
            }
            set
            {
                _lpu.Assign(value);
            }
        }

*/        

        /// <remarks/>
        [Column (IsPrimaryKey = true)]
        public long ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
      //  [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        [Column]
        public long Parent
        {
            get
            {
                return this.parentField;
            }
            set
            {
                this.parentField = value;
            }
        }

        /// <remarks/>
        [Column]
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
            info.AddValue("ID", idField);
            info.AddValue("Parent", parentField);
            info.AddValue("Name",nameField);
        }
        /*
        public Level (SerializationInfo info, StreamingContext context)
        {
            idField = (int) info.GetValue("ID", typeof (int));
            parentField = (int?) info.GetValue("Parent", typeof (int?));
            nameField = (string)info.GetValue("Name", typeof(string));
        }
         
        public  Level (int ID, int? Parent, string Name)
        {
            this.idField = ID;
            this.parentField = Parent;
            this.nameField = Name;
        }
        */
        public  Level ()
        {
        }

        public List<Classes.Level> getLevelList(Classes.ArrayOfStaffEnrty staffEntryCollection)
        {

            var sLpu = new List<Classes.LPU>(from sc in staffEntryCollection select sc.UZ);

            var sLevel = new List<Classes.Level>(from lv in sLpu select lv.LPULevel);

            var ssSub = sLevel
                .GroupBy(s => new { s.ID })
                //.GroupBy(s => new { s.ID, s.Name, s.Parent })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }

        public List<Classes.Level> getLevelList(List<Classes.LPU> sLpu)
        {

           var sLevel = new List<Classes.Level>(from lv in sLpu select lv.LPULevel);

            var ssSub = sLevel
                .GroupBy(s => new { s.ID })
                //.GroupBy(s => new { s.ID, s.Name, s.Parent })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }

        
    }

    
}
